package cn.rongdao.jrkabao.ui.main.mainfragment.vip;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import com.mifengkong.frtools.http.request.callback.FRBaseObserver;
import com.mifengkong.frtools.ui.activity.FRStartActivity;
import com.mifengkong.frwidget.recyclerview.PtrRecyclerViewUIComponent;
import com.mifengkong.frwidget.recyclerview.divider.RecyclerViewDivider;
import com.mifengkong.frwidget.refersh.OnPullToRefreshListener;
import com.mifengkong.frwidget.titlebar.TitleBarUIComponent;

import java.util.List;

import butterknife.BindView;
import cn.rongdao.jrkabao.R;
import cn.rongdao.jrkabao.base.BaseAutoFinishActivity;
import cn.rongdao.jrkabao.bean.main.vip.VipPurchaseRecordBean;
import cn.rongdao.jrkabao.model.main.vip.VipPurchaseRecordModel;
import cn.rongdao.jrkabao.ui.cashadvance.uicomponent.CashEmptyUIComponent;
import cn.rongdao.jrkabao.ui.main.mainfragment.adapter.VipPurchaseRecordAdapter;
import retrofit2.http.HEAD;

/**
 * Created by jack on 2018/1/26
 */

public class VipPurchaseRecordActivity extends BaseAutoFinishActivity {

    public static void start(Context context) {
        FRStartActivity.start(context, VipPurchaseRecordActivity.class);
    }

    @BindView(R.id.avpr_titlebar)
    TitleBarUIComponent titleBarUIComponent;
    @BindView(R.id.avpr_ptr_framelayout)
    PtrRecyclerViewUIComponent ptrRecyclerViewUIComponent;
    @BindView(R.id.avpr_empty_view)
    CashEmptyUIComponent cashEmptyUIComponent;

    private VipPurchaseRecordAdapter adapter;

    @Override
    protected int layoutRes() {
        return R.layout.activity_vip_purchase_record;
    }

    @Override
    protected void initView() {
        super.initView();
        initTitleBar();
        initPtr();
    }

    private void initPtr() {
        adapter = new VipPurchaseRecordAdapter(thisActivity());
        ptrRecyclerViewUIComponent.delayRefresh();
        ptrRecyclerViewUIComponent.setLayoutManager(new LinearLayoutManager(thisActivity()));
        ptrRecyclerViewUIComponent.setRecyclerViewDivider(new RecyclerViewDivider(thisActivity()));
        ptrRecyclerViewUIComponent.setAdapter(adapter);
        ptrRecyclerViewUIComponent.setOnPullToRefreshListener(new OnPullToRefreshListener() {
            @Override
            public void onPullToRefresh() {
                getPurchaseRecordData();
            }
        });
    }

    private void getPurchaseRecordData() {
        VipPurchaseRecordModel.getInstance().execute(new FRBaseObserver<List<VipPurchaseRecordBean>>(this, "加载中，请稍候...") {
            @Override
            protected void onBaseNext(List<VipPurchaseRecordBean> data) {
                if (null != data && data.size() > 0) {
                    refreshComplete(false);
                    adapter.setDataList(data);
                } else {
                    refreshComplete(true);
                }
            }

            @Override
            protected void onBaseError(Throwable t) {
                super.onBaseError(t);
                refreshComplete(true);
            }
        });
    }

    private void refreshComplete(boolean isEmpty) {
        if (!isEmpty) {
            ptrRecyclerViewUIComponent.setVisibility(View.VISIBLE);
            cashEmptyUIComponent.setVisibility(View.GONE);
        } else {
            ptrRecyclerViewUIComponent.refreshComplete();
            ptrRecyclerViewUIComponent.setVisibility(View.GONE);
            cashEmptyUIComponent.initEmptyView("还没有购买记录哦～");
        }
        ptrRecyclerViewUIComponent.refreshComplete();
    }

    private void initTitleBar() {
        titleBarUIComponent.initTitle("购买记录");
        titleBarUIComponent.setBackgroundColor(ContextCompat.getColor(this, R.color.colorPrimary));
        titleBarUIComponent.initLeftBack(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }
}
